/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../data_struct/head.h"
#include "../helper_objs/head.h"
#include "../config/head.h"
#include "../impl/head.h"
#include "../macro/head.h"
#include "../msg/head.h"
#include "request.h"

namespace xos_http
{

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    static request::ITEM_DATA xos_item_data;

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    request::request()
    {
        init_data();
    }

    request::~request()
    {}

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int request::get_xos_item_data( ITEM_DATA*& item_ptr )
    {
        int ret = 0;
        xos_item_data.set_container( mgr::container() );
        item_ptr = &xos_item_data;
        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int request::set_connect( http::i_connect * pConnect )
    {
        int ret = 0;
        m_pHttpParse->set_recv_list( pConnect->recv_list() );
        m_pConnect = (connection *)pConnect;
        return ret;
    }

    http::i_connect * request::get_connect()
    {
        return m_pConnect;
    }

    xos_box::i_str_property * request::params()
    {
        return m_pParams;
    }

    xos_box::i_int_property * request::paths()
    {
        return m_pPaths;
    }

    xos_box::i_str_property * request::tags()
    {
        return m_pTags;
    }

    xos_box::i_list * request::cookies()
    {
        return m_pCookieList;
    }

    xos_box::i_list * request::bodys()
    {
        return m_pBodyList;
    }

    http::i_session * request::session( http::i_session * pSession )
    {
        if( pSession )
        {
            m_pSession = pSession;
        }
        return m_pSession;
    }

    const char * request::cookie( const char * lpszName )
    {
        const char * pRet = 0;
        xos_box::i_list_iter * pIter = m_pCookieList->begin();
        xos::i_crt * pCrt = mgr::container()->crt();
        http::i_cookie * pC = 0;
        while( ( pC = (http::i_cookie *)pIter->next() ) )
        {
            if( 0 == pCrt->strcmp( lpszName, pC->name( 0 ) ) )
            {
                pRet = pC->value( 0 );
                break;
            }
        }
        xos_stl::release_interface( pIter );
        return pRet;
    }

    bool request::ssl()
    {
        return m_bSSL;
    }

    int request::parse( xos_box::i_msg *& pMsg )
    {
        int ret = 0;

        task::T * pTask = (task::T *)pMsg->get_void( 0, 0 );
        connection * pConnect = m_pConnect;
        net::i_connect * pNetCon = pConnect->get_net_connect();

        ret = m_pHttpParse->proc();
        int nMsg = 0;

        if( 0 == ret )
        {
            pConnect->m_task_list.push_back( pTask );
            pConnect->m_pRecvTask = 0;
            pTask = 0;
            nMsg = FAST_PROCESS;
        }
        else if( -1 == ret )
        {
            pConnect->m_bHttpFailed = true;
            nMsg = FAST_PARSE_FAILED;
        }

        if( 0 != nMsg )
        {
            pMsg->set_void( 0, pConnect );
            pConnect->fast_notify()->notify( pMsg, nMsg );
        }
        else
        {
            pNetCon->release_ref();
        }

        return ret;
    }

    xos_box::i_str_property * request::prop()
    {
        return m_pProperty;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int request::init_data()
    {
        int ret = 0;
        
        m_pHttpParse = 0;
        m_pConnect = 0;
        m_pTask = 0;

        m_pParams = 0;
        m_pPaths = 0;
        m_pTags = 0;
        m_pCookieList = 0;
        m_pBodyList = 0;
        m_pSession = 0;
        m_bSSL = false;

        m_pProperty = 0;

        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //

    int request::init()
    {
        int ret = 0;
        if( 0 == ret )
        {
            m_pPaths = mgr::container()->box()->get_int_prop();
            m_pParams = mgr::container()->box()->get_str_prop();
            m_pTags = mgr::container()->box()->get_str_prop();
            m_pCookieList = mgr::container()->box()->list();
            m_pBodyList = mgr::container()->box()->list();
        }
        if( 0 == ret )
        {
            m_pHttpParse = mgr::container()->http()->parse_request();
            m_pHttpParse->set_param_property( m_pParams );
            m_pHttpParse->set_path_property( m_pPaths );
            m_pHttpParse->set_tag_property( m_pTags );
            m_pHttpParse->set_cookie_list( m_pCookieList );
            m_pHttpParse->set_body_list( m_pBodyList );
            m_pHttpParse->set_session( &m_pSession );
            m_pHttpParse->set_ssl( m_bSSL );
        }
        if( 0 == ret )
        {
            m_pProperty = mgr::container()->box()->get_str_prop();
            prop()->set_str_prop( "params", params(), false );
            prop()->set_int_prop( "paths", paths(), false );
            prop()->set_str_prop( "tags", tags(), false );
        }
        return ret;
    }

    int request::term()
    {
        int ret = 0;
        xos_stl::release_interface( m_pProperty );
        xos_stl::release_interface( m_pCookieList );
        xos_stl::release_interface( m_pBodyList );
        xos_stl::release_interface( m_pParams );
        xos_stl::release_interface( m_pPaths );
        xos_stl::release_interface( m_pTags );
        xos_stl::release_interface( m_pHttpParse );
        xos_stl::release_ref( m_pSession );
        init_data();
        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //

} // xos_http
